package com.zds.scm.dao.impl;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.zds.scm.common.dao.AbstractDAOImpl;
import com.zds.scm.common.dao.DaoException;
import com.zds.scm.dao.TeacherInfoDAO;
import com.zds.scm.domain.TeacherInfo;

public class TeacherInfoDAOImpl extends AbstractDAOImpl<TeacherInfo> implements TeacherInfoDAO {

	@Override
	protected TeacherInfo rowMapper(ResultSet rs) throws SQLException {
		TeacherInfo teacherInfo=new TeacherInfo();
		try {
			teacherInfo.setTeacherCode(rs.getString("Teacher_Code"));
			teacherInfo.setTeacherName(rs.getString("Teacher_Name"));
			teacherInfo.setTeacherMobile(rs.getString("Teacher_Mobile"));
			teacherInfo.setDeptCode(rs.getString("Dept_Code"));
		} catch (Exception e) {//在DAO层不处理异常,直接抛出到外层
			logger.error("DAO层结果集映射处理错误:"+e.getMessage());
			throw new DaoException(e.getMessage(),e);
		}
  		return teacherInfo;
	}

	@Override
	public boolean delete(TeacherInfo teacherInfo) {
		String sql="delete from teacherinfo where teacher_code=?";
		Object[] args=new Object[]{teacherInfo.getTeacherCode()};
 		return super.updateEntity(sql, args)>0;
	}

	@Override
	public boolean insert(TeacherInfo teacherInfo) {
		String sql="insert into teacherinfo(Dept_Code,Teacher_Code,Teacher_Mobile,Teacher_Name) values(?,?,?,?)";
		Object[] args=new Object[]{teacherInfo.getDeptCode(),teacherInfo.getTeacherCode(),teacherInfo.getTeacherMobile(),teacherInfo.getTeacherName()};
 		return super.updateEntity(sql, args)>0;
	}

	@Override
	public boolean update(TeacherInfo teacherInfo) {
		String sql="update teacherinfo set Dept_Code=?,Teacher_Mobile=?,Teacher_Name=? where Teacher_Code=?";
		Object[] args=new Object[]{teacherInfo.getDeptCode(),teacherInfo.getTeacherMobile(),teacherInfo.getTeacherName(),teacherInfo.getTeacherCode()};
 		return super.updateEntity(sql, args)>0;
	}

	@Override
	public TeacherInfo findById(Serializable id) {
		String sql="select * from teacherinfo where teacher_code=?";
		Object[] args=new Object[]{id};
		return super.findEntity(sql, args);
	}
	@Override
	public List<TeacherInfo> findAll() {
 		return findAll(-1,-1);
	}
	@Override
	public List<TeacherInfo> findAll(int currentPage, int pageSize) {
		String sql="select * from teacherInfo";
		Object[] args=null;
		if(currentPage<1||pageSize<1){
			args=new Object[]{};
		}else {
			sql+=" limit ?,?";
			args=new Object[]{(currentPage-1)*pageSize,pageSize};
		}
		return super.findEntitys(sql, args);
	}

	@Override
	public int getTotalRows() {
		String sql="select count(*) from teacherInfo";
		Object[] args=new Object[]{};
		return super.findEntityCount(sql, args);
	}
	
	@Override
	public List<TeacherInfo> findByDeptCode(String DeptCode, int currentPage,int pageSize) {
		String sql="select * from teacherInfo where dept_code=?";
		Object[] args=null;
		if(currentPage<1||pageSize<1){
			args=new Object[]{DeptCode};
		}else {
			sql+=" limit ?,?";
			args=new Object[]{DeptCode,(currentPage-1)*pageSize,pageSize};
		}
		return super.findEntitys(sql, args);
	}
	
	@Override
	public int getTotalRowsByDeptCode(String DeptCode) {
		String sql="select count(*) from teacherInfo where dept_code=?";
		Object[] args=new Object[]{DeptCode};
		return super.findEntityCount(sql, args);
	}
	@Override
	public List<TeacherInfo> findByCode(String likeTeacherCode,			int currentPage, int pageSize) {
		String sql="select * from teacherInfo where teacher_code like ?";
		Object[] args=null;
		if(currentPage<1||pageSize<1){
			args=new Object[]{"%"+likeTeacherCode+"%"};
		}else {
			sql+=" limit ?,?";
			args=new Object[]{"%"+likeTeacherCode+"%",(currentPage-1)*pageSize,pageSize};
		}
		return super.findEntitys(sql, args);
	}
	@Override
	public int getTotalRowsByCode(String likeTeacherCode) {
		String sql="select count(*) from teacherInfo where teacher_code like ?";
		Object[] args=new Object[]{"%"+likeTeacherCode+"%"};
		return super.findEntityCount(sql, args);
	}
	@Override
	public List<TeacherInfo> findByName(String likeTeacherName,int currentPage, int pageSize) {
		String sql="select * from teacherInfo where teacher_Name like ?";
		Object[] args=null;
		if(currentPage<1||pageSize<1){
			args=new Object[]{"%"+likeTeacherName+"%"};
		}else {
			sql+=" limit ?,?";
			args=new Object[]{"%"+likeTeacherName+"%",(currentPage-1)*pageSize,pageSize};
		}
		return super.findEntitys(sql, args);
	}
	@Override
	public int getTotalRowsByName(String likeTeacherName) {
		String sql="select count(*) from teacherInfo where teacher_Name like ?";
		Object[] args=new Object[]{"%"+likeTeacherName+"%"};
		return super.findEntityCount(sql, args);
	}
}
